//
//  COMineHeaderView.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/23.
//

import UIKit

class COMineHeaderView: UIView {
    
    var userInfo: COUserInfo? {
        didSet {
            guard let userInfo = userInfo else {return}
            
            imageView.kf.setImage(with: URL(string: userInfo.sculpture))
            avatarImageView.kf.setImage(with: URL(string: userInfo.sculpture))
            if userInfo.level.isEmpty {
                statusImageView.isHidden = true
            } else {
                statusImageView.isHidden = false
                var name = ""
                if userInfo.level == "Intermediate" {
                    name = "编组 2备份 3"
                } else if userInfo.level == "Beginner" {
                    name = "编组 2备份 5"
                } else {
                    name = "编组 16"
                }
                statusImageView.image = UIImage(named: name)
                
            }
            
            nameLabel.text = userInfo.nickName
            idLabel.text = "(ID:\(userInfo.userId))"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func configUI() {
        backgroundColor = .rgba(17, 7, 41, 1)
        addSubview(imageView)
        addSubview(avatarImageView)
        addSubview(statusImageView)
//        addSubview(yelloImageView)
//        addSubview(tagImageView)
        addSubview(nameLabel)
        addSubview(idLabel)
        
        imageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(112+statusBarHeight)
        }
        
        avatarImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(-47)
            make.width.height.equalTo(97)
        }
        
        statusImageView.snp.makeConstraints { make in
            make.bottom.equalTo(avatarImageView.snp.bottom).offset(8)
            make.centerX.equalTo(avatarImageView)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(statusImageView.snp.bottom).offset(9)
        }
        
        idLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(2)
        }
    }
    
    private lazy var imageView: UIImageView = {
        let imgV = UIImageView()
        imgV.clipsToBounds = true
        imgV.contentMode = .scaleAspectFill
        return imgV
    }()
    
    private lazy var avatarImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 47
        return imgV
    }()
    
    private lazy var statusImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "编组 2备份 3"))
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .bold(22)
        label.textColor = .white
        return label
    }()
    
    private lazy var idLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(13)
        label.textColor = .white.withAlphaComponent(0.5)
        return label
    }()
}
